package com.mingtu.jgspatrol.view.popupview;

import static com.mingtu.common.utils.MyUtills.getBounds;
import static com.mingtu.jgspatrol.utils.MyConstant.INFO_WINDOW_OFF_SET_Y;
import static com.mingtu.jgspatrol.utils.MyConstant.INFO_WINDOW_OFF_SET_Y2;
import static com.mingtu.jgspatrol.utils.MyConstant.MAP_FACTORY_PADDING;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.view.View;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polygon;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lxj.xpopup.core.BottomPopupView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.OnlinePeopleUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.adapter.BasicLayerAdapter;
import com.mingtu.jgspatrol.adapter.HardwareLayerAdapter;
import com.mingtu.jgspatrol.bean.EcDequiBean;
import com.mingtu.jgspatrol.bean.ProjectDeptBean;
import com.mingtu.jgspatrol.bean.VideoListBean2;
import com.mingtu.jgspatrol.databinding.LayoutBottomPopupMapLayerBinding;
import com.mingtu.jgspatrol.utils.GetJsonDataUtil;
import com.mingtu.jgspatrol.utils.MarkerOptionsUtils;
import com.mingtu.jgspatrol.utils.MyConstant;
import com.mingtu.infrared.bean.InfraredDequiBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2023-10-18 16:38:51
 * @Email : 1281573890@qq.com
 * @Eescription :
 */
public class BottomPopupLayer extends BottomPopupView {
    private AMap aMap;
    private Timer mTimer;
    private Context context;
    private TimerTask mTimerTask;
    private BasicLayerAdapter basicLayerAdapter;
    private OnStatusListener onStatusListener = null;
    private HardwareLayerAdapter hardwareLayerAdapter;
    private OnCheckedChangeListener onCheckedChangeListener = null;

    private List<Polygon> areaPolygon1 = new CopyOnWriteArrayList<>();
    private List<Polygon> areaPolygon2 = new CopyOnWriteArrayList<>();
    private List<Polygon> areaPolygon3 = new CopyOnWriteArrayList<>();
    private List<LatLng> areaAllLatLng = new CopyOnWriteArrayList<>();

    private List<Marker> infraredMarker = new CopyOnWriteArrayList<>(); //红外
    private List<Marker> cameraMarkers = new CopyOnWriteArrayList<>(); //所有相机
    private List<Marker> cameraMarkers1 = new CopyOnWriteArrayList<>(); //卡扣相机
    private List<Marker> cameraMarkers2 = new CopyOnWriteArrayList<>(); //森林防火
    private List<Marker> cameraMarkers3 = new CopyOnWriteArrayList<>(); //制高点
    private List<Marker> cameraMarkers4 = new CopyOnWriteArrayList<>(); //科研监测
    private List<Marker> weatherMarkers = new CopyOnWriteArrayList<>(); //气象
    private List<Marker> hydrologyMarkers = new CopyOnWriteArrayList<>(); //水文
    private List<Marker> projectDeptMarkers = new CopyOnWriteArrayList<>(); //保护站

    private List<Marker> deviceMarker;
    private List<LatLng> weatherLatLngs;
    private List<LatLng> infraredLatLngs;
    private List<LatLng> hydrologyLatLngs;
    private List<LatLng> cameraLatLngs;
    private List<LatLng> cameraFireLatLngs;
    private List<LatLng> projectDeptLatLngs;
    private List<LatLng> cameraHeightLatLngs;
    private List<LatLng> cameraBayonetLatLngs;
    private List<LatLng> cameraScientificLatLngs;
    private LayoutBottomPopupMapLayerBinding popupBinding;

    private String[] cameraType = {"森林防火", "卡口", "制高点", "科研监测"};

    public BottomPopupLayer(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public BottomPopupLayer(@NonNull Context context, AMap amap, List<LatLng> areaAllLatLng,
                            List<Polygon> areaPolygon1, List<Polygon> areaPolygon2, List<Polygon> areaPolygon3) {
        super(context);
        this.context = context;
        this.aMap = amap;
        this.areaAllLatLng = areaAllLatLng;
        this.areaPolygon1 = areaPolygon1;
        this.areaPolygon2 = areaPolygon2;
        this.areaPolygon3 = areaPolygon3;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_bottom_popup_map_layer;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        popupBinding = LayoutBottomPopupMapLayerBinding.bind(bottomPopupContainer.getChildAt(0));
        if (hardwareLayerAdapter == null) {
            hardwareLayerAdapter = new HardwareLayerAdapter(context, popupBinding.mgvHardware);
            popupBinding.mgvHardware.setAdapter(hardwareLayerAdapter);
        }
        hardwareLayerAdapter.setOnItemClickListener(new HardwareLayerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, boolean isChecked) {
//                dismiss();

                if (isChecked) {
                    if (position == 0) {
                        //红外
                        getInfraredDequiList();
                    }

                    if (position == 1) {
                        //卡口
                        getCameraList();
                    }

                    if (position == 2) {
                        getDequiList(0);
                    }

                    if (position == 3) {
                        getDequiList(1);
                    }

                } else {
                    if (position == 0) {
                        if (infraredMarker.size() > 0) {
                            for (Marker marker : infraredMarker) {
                                marker.setVisible(false);
                            }
                        }

                    }

                    if (position == 1) {
                        //卡口
                        if (cameraMarkers.size() > 0) {
                            for (Marker marker : cameraMarkers) {
                                marker.setVisible(false);
                            }
                        }
                    }

                    if (position == 2) {
                        if (weatherMarkers.size() > 0) {
                            for (Marker marker : weatherMarkers) {
                                marker.setVisible(false);
                            }
                        }
                    }
                    if (position == 3) {
                        if (hydrologyMarkers.size() > 0) {
                            for (Marker marker : hydrologyMarkers) {
                                marker.setVisible(false);
                            }
                        }
                    }
                }


            }
        });

        if (basicLayerAdapter == null) {
            basicLayerAdapter = new BasicLayerAdapter(context, popupBinding.mgvBasic);
            popupBinding.mgvBasic.setAdapter(basicLayerAdapter);
        }
        basicLayerAdapter.setOnItemClickListener(new BasicLayerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, boolean isChecked) {
//                dismiss();


                if (position == 1) {
                    for (Polygon polygon : areaPolygon1) {
                        polygon.setVisible(isChecked);
                    }
                }
                if (position == 2) {
                    for (Polygon polygon : areaPolygon2) {
                        polygon.setVisible(isChecked);
                    }
                }
                if (position == 3) {
                    for (Polygon polygon : areaPolygon3) {
                        polygon.setVisible(isChecked);
                    }
                }

                if (position == 1 || position == 2 || position == 3) {
                    if (areaAllLatLng.size() > 0) {
                        aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(areaAllLatLng), 200));
                    }
                }

                if (isChecked) {
//                        if (position == 0) {
//                            getDeptList();
//                        }

                    if (position == 4) {
                        OnlinePeopleUtils.getInstance(aMap).startGetDeviceData();
                    }
                } else {
//                        if (position == 0) {
//                            if (projectDeptMarkers.size() > 0) {
//                                for (Marker marker : projectDeptMarkers) {
//                                    marker.setVisible(false);
//                                }
//                            }
//
//                        }

                    if (position == 4) {
                        OnlinePeopleUtils.getInstance(aMap).stopGetDeviceData();
                    }
                }


            }
        });


        popupBinding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (onCheckedChangeListener != null)
                    onCheckedChangeListener.onCheckedChanged(group, checkedId);
                switch (checkedId) {
                    case R.id.rb_map1:
                        popupBinding.tvMap1.setSelected(true);
                        popupBinding.tvMap2.setSelected(false);
                        break;
                    case R.id.rb_map2:
                        popupBinding.tvMap1.setSelected(false);
                        popupBinding.tvMap2.setSelected(true);
                        break;
                }
            }
        });


    }

    @Override
    protected void onDismiss() {
        if (onStatusListener != null) {
            onStatusListener.onDismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        popupBinding = null;
        OnlinePeopleUtils.getInstance(aMap).stopGetDeviceData();
    }

    /**
     * 气象、水文设备列表
     */
    private void getDequiList(int type) {
        if (type == 0) {
            //气象
            if (weatherMarkers.size() > 0) {
                for (Marker marker : weatherMarkers) {
                    marker.setVisible(true);
                }

                if (weatherLatLngs.size() > 0) {
                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(weatherLatLngs), 200));
                }
                return;
            }
        }

        if (type == 1) {
            //水文
            if (hydrologyMarkers.size() > 0) {
                for (Marker marker : hydrologyMarkers) {
                    marker.setVisible(true);
                }

                if (hydrologyLatLngs.size() > 0) {
                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(hydrologyLatLngs), 200));
                }
                return;
            }
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceType", null);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_EC_DEVICE)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            EcDequiBean ecDequiBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                ecDequiBean = new EcDequiBean();
                                ecDequiBean = gson.fromJson(result, EcDequiBean.class);
                                List<EcDequiBean.DataBean> dataBeans = ecDequiBean.getData();

                                if (dataBeans != null) {
                                    int size = dataBeans.size();
                                    if (size > 0) {

                                        //气象
                                        ApplicationInfo appInfo1 = context.getApplicationInfo();
                                        ArrayList<BitmapDescriptor> weatherGif = new ArrayList<BitmapDescriptor>();
                                        for (int i = 1; i <= 24; i++) {
                                            int resID = getResources().getIdentifier("icon_ec" + i, "mipmap", appInfo1.packageName);
                                            weatherGif.add(BitmapDescriptorFactory.fromResource(resID));
                                        }


                                        //水文
                                        ApplicationInfo appInfo2 = context.getApplicationInfo();
                                        ArrayList<BitmapDescriptor> hydrologyGif = new ArrayList<BitmapDescriptor>();
                                        for (int i = 1; i <= 24; i++) {
                                            int resID = getResources().getIdentifier("icon_hydrology" + i, "mipmap", appInfo2.packageName);
                                            hydrologyGif.add(BitmapDescriptorFactory.fromResource(resID));
                                        }


                                        weatherLatLngs = new ArrayList<>();
                                        hydrologyLatLngs = new ArrayList<>();

                                        for (int i = 0; i < size; i++) {
                                            EcDequiBean.DataBean dataBean = dataBeans.get(i);
                                            String latitude = dataBean.getLat();
                                            String longitude = dataBean.getLon();
                                            String heartTime = dataBean.getHeartTime();
                                            String status = dataBean.getStatus();
                                            String name = dataBean.getName();


                                            boolean isOnline = false;
                                            if (!StringUtils.isEmpty(status)) {
                                                if (status.equals("0")) { //0 离线  1在线
                                                    //如果当前时间与心跳时间的差值大于12小时，说明设备离线
                                                    isOnline = false;
                                                } else {
                                                    isOnline = true;
                                                }

                                            } else {
                                                long hearTimeMillis = heartTime != null ? com.blankj.utilcode.util.TimeUtils.string2Millis(heartTime) : 0;
                                                long nowMills = com.blankj.utilcode.util.TimeUtils.getNowMills();
                                                if (nowMills - hearTimeMillis > 12 * 60 * 60 * 1000) {
                                                    //如果当前时间与心跳时间的差值大于12小时，说明设备离线
                                                    isOnline = false;
                                                } else {
                                                    isOnline = true;
                                                }

                                            }

                                            if (!StringUtils.isEmpty(latitude) && !StringUtils.isEmpty(longitude)) {
                                                double lat = Double.parseDouble(latitude);
                                                double lng = Double.parseDouble(longitude);
                                                LatLng location = new LatLng(lat, lng);
                                                //添加Marker
                                                if (isOnline) {
                                                    if (type == 0) {
                                                        if (name.indexOf("气象") != -1) {
                                                            Marker marker1 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions2(location, name, INFO_WINDOW_OFF_SET_Y, weatherGif));
                                                            weatherMarkers.add(marker1);
                                                            weatherLatLngs.add(location);
                                                        }
                                                    } else {
                                                        Marker marker2 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions2(location, name, INFO_WINDOW_OFF_SET_Y, hydrologyGif));
                                                        hydrologyMarkers.add(marker2);
                                                        hydrologyLatLngs.add(location);
                                                    }

                                                } else {
                                                    if (type == 0) {
                                                        if (name.indexOf("气象") != -1) {
                                                            Marker marker1 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions1(location, name, INFO_WINDOW_OFF_SET_Y, R.mipmap.icon_ec_offline));
                                                            weatherMarkers.add(marker1);
                                                            weatherLatLngs.add(location);
                                                        }
                                                    } else {
                                                        Marker marker2 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions1(location, name, INFO_WINDOW_OFF_SET_Y, R.mipmap.icon_hydrology_offline));
                                                        hydrologyMarkers.add(marker2);
                                                        hydrologyLatLngs.add(location);
                                                    }
                                                }
                                            }


                                        }

                                        if (type == 0) {
                                            if (weatherLatLngs.size() > 0) {
                                                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(weatherLatLngs), MAP_FACTORY_PADDING));
                                            }
                                        }

                                        if (type == 1) {
                                            if (hydrologyLatLngs.size() > 0) {
                                                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(hydrologyLatLngs), MAP_FACTORY_PADDING));
                                            }
                                        }

                                    }
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }


    /**
     * 分类获取摄像头列表
     */
    private void getCameraList(String type) {

        if (type.equals(cameraType[0])) {
            if (cameraMarkers1.size() > 0) {
                for (Marker marker : cameraMarkers1) {
                    marker.setVisible(true);
                }

                if (cameraFireLatLngs.size() > 0) {
                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraFireLatLngs), 200));
                }
                return;
            }
        }


        if (type.equals(cameraType[1])) {
            if (cameraMarkers2.size() > 0) {
                for (Marker marker : cameraMarkers2) {
                    marker.setVisible(true);
                }

                if (cameraBayonetLatLngs.size() > 0) {
                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraBayonetLatLngs), 200));
                }
                return;
            }
        }


        if (type.equals(cameraType[2])) {
            if (cameraMarkers3.size() > 0) {
                for (Marker marker : cameraMarkers3) {
                    marker.setVisible(true);
                }

                if (cameraHeightLatLngs.size() > 0) {
                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraHeightLatLngs), 200));
                }
                return;
            }
        }

        if (type.equals(cameraType[3])) {
            if (cameraMarkers4.size() > 0) {
                for (Marker marker : cameraMarkers4) {
                    marker.setVisible(true);
                }

                if (cameraScientificLatLngs.size() > 0) {
                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraScientificLatLngs), 200));
                }
                return;
            }
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("curPage", 1);
        hashMap.put("pageSize", 100);
        hashMap.put("type", type);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(Constant.getInstance().POST_CAMERA_LIST)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            VideoListBean2 videoListBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                videoListBean = new VideoListBean2();
                                videoListBean = gson.fromJson(result, VideoListBean2.class);
                                List<VideoListBean2.PageBean.ListBean> dataBeans = videoListBean.getPage().getList();

                                if (dataBeans != null) {
                                    if (dataBeans.size() > 0) {

                                        if (type.equals(cameraType[0])) {
                                            cameraFireLatLngs = new ArrayList<>();
                                        }

                                        if (type.equals(cameraType[1])) {
                                            cameraBayonetLatLngs = new ArrayList<>();
                                        }

                                        if (type.equals(cameraType[2])) {
                                            cameraHeightLatLngs = new ArrayList<>();
                                        }

                                        if (type.equals(cameraType[3])) {
                                            cameraScientificLatLngs = new ArrayList<>();
                                        }
                                        //森林防火
                                        ArrayList<BitmapDescriptor> cameraFirGif = new ArrayList<BitmapDescriptor>();
                                        ApplicationInfo appInfo1 = context.getApplicationInfo();
                                        for (int i = 1; i <= 24; i++) {
                                            int resID = getResources().getIdentifier("icon_camera_fire" + i, "mipmap", appInfo1.packageName);
                                            cameraFirGif.add(BitmapDescriptorFactory.fromResource(resID));
                                        }

                                        //卡口
                                        ArrayList<BitmapDescriptor> cameraBayonetGif = new ArrayList<BitmapDescriptor>();
                                        ApplicationInfo appInfo2 = context.getApplicationInfo();
                                        for (int i = 1; i <= 24; i++) {
                                            int resID = getResources().getIdentifier("icon_camera_bayonet" + i, "mipmap", appInfo2.packageName);
                                            cameraBayonetGif.add(BitmapDescriptorFactory.fromResource(resID));
                                        }
                                        //制高点
                                        ArrayList<BitmapDescriptor> cameraHeightGif = new ArrayList<BitmapDescriptor>();
                                        ApplicationInfo appInfo3 = context.getApplicationInfo();
                                        for (int i = 1; i <= 24; i++) {
                                            int resID = getResources().getIdentifier("icon_camera_heigh" + i, "mipmap", appInfo3.packageName);
                                            cameraHeightGif.add(BitmapDescriptorFactory.fromResource(resID));
                                        }

                                        //科研
                                        ArrayList<BitmapDescriptor> cameraScientificGif = new ArrayList<BitmapDescriptor>();
                                        ApplicationInfo appInfo4 = context.getApplicationInfo();
                                        for (int i = 1; i <= 24; i++) {
                                            int resID = getResources().getIdentifier("icon_camera_scientific" + i, "mipmap", appInfo4.packageName);
                                            cameraScientificGif.add(BitmapDescriptorFactory.fromResource(resID));
                                        }


                                        for (int i = 0; i < dataBeans.size(); i++) {
                                            VideoListBean2.PageBean.ListBean listBean = dataBeans.get(i);
                                            String latitude = listBean.getLat();
                                            String longitude = listBean.getLng();
                                            String status = listBean.getStatus();
                                            String name = listBean.getName();

                                            if (StringUtils.isEmpty(status)) {
                                                status = "1"; //为空的话 暂时赋值为离线状态
                                            }

                                            //过滤掉经纬度为空的数据
                                            if (!StringUtils.isEmpty(latitude) && !StringUtils.isEmpty(longitude)) {
                                                double lat = Double.parseDouble(latitude);
                                                double lng = Double.parseDouble(longitude);
                                                LatLng location = new LatLng(lat, lng);

                                                //添加Marker
                                                if (status.equals("0")) {
                                                    if (type.equals(cameraType[0])) {
                                                        //森林防火
                                                        Marker marker1 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions2(location, name, INFO_WINDOW_OFF_SET_Y2, cameraFirGif));
                                                        cameraMarkers1.add(marker1);
                                                        cameraFireLatLngs.add(location);
                                                    }

                                                    if (type.equals(cameraType[1])) {
                                                        //卡口
                                                        Marker marker2 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions2(location, name, INFO_WINDOW_OFF_SET_Y, cameraBayonetGif));
                                                        cameraMarkers2.add(marker2);
                                                        cameraBayonetLatLngs.add(location);
                                                    }

                                                    if (type.equals(cameraType[2])) {
                                                        //制高点
                                                        Marker marker3 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions2(location, name, INFO_WINDOW_OFF_SET_Y, cameraHeightGif));
                                                        cameraMarkers3.add(marker3);
                                                        cameraHeightLatLngs.add(location);
                                                    }

                                                    if (type.equals(cameraType[3])) {
                                                        //科研
                                                        Marker marker4 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions2(location, name, INFO_WINDOW_OFF_SET_Y, cameraScientificGif));

                                                        cameraMarkers4.add(marker4);
                                                        cameraScientificLatLngs.add(location);
                                                    }

                                                } else if (status.equals("1")) {

                                                    if (type.equals(cameraType[0])) {
                                                        //森林防火
                                                        Marker marker11 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions1(location, name, INFO_WINDOW_OFF_SET_Y2, R.mipmap.icon_camera_fire_offline));
                                                        cameraMarkers1.add(marker11);
                                                        cameraFireLatLngs.add(location);
                                                    }


                                                    if (type.equals(cameraType[1])) {
                                                        //卡口
                                                        Marker marker22 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions1(location, name, INFO_WINDOW_OFF_SET_Y, R.mipmap.icon_camera_bayonet_offline));
                                                        cameraMarkers2.add(marker22);
                                                        cameraBayonetLatLngs.add(location);
                                                    }


                                                    if (type.equals(cameraType[2])) {
                                                        //制高点
                                                        Marker marker33 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions1(location, name, INFO_WINDOW_OFF_SET_Y, R.mipmap.icon_camera_height_offline));

                                                        cameraMarkers3.add(marker33);
                                                        cameraHeightLatLngs.add(location);
                                                    }

                                                    if (type.equals(cameraType[3])) {
                                                        //科研
                                                        Marker marker44 = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions1(location, name, INFO_WINDOW_OFF_SET_Y, R.mipmap.icon_camera_scientific_offline));
                                                        cameraMarkers4.add(marker44);
                                                        cameraScientificLatLngs.add(location);
                                                    }


                                                }
                                            }

                                            if (type.equals(cameraType[0])) {
                                                if (cameraFireLatLngs.size() > 0) {
                                                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraFireLatLngs), MAP_FACTORY_PADDING));
                                                }
                                            }

                                            if (type.equals(cameraType[1])) {
                                                if (cameraBayonetLatLngs.size() > 0) {
                                                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraBayonetLatLngs), MAP_FACTORY_PADDING));
                                                }
                                            }

                                            if (type.equals(cameraType[2])) {
                                                if (cameraHeightLatLngs.size() > 0) {
                                                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraHeightLatLngs), MAP_FACTORY_PADDING));
                                                }
                                            }

                                            if (type.equals(cameraType[3])) {
                                                if (cameraScientificLatLngs.size() > 0) {
                                                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraScientificLatLngs), MAP_FACTORY_PADDING));
                                                }
                                            }
                                        }
                                    }
                                }

                            } catch (JsonSyntaxException e) {

                                e.printStackTrace();

                                ToastUtils.showLong(getResources().getString(R.string.text_json_error));

                            }


                        }
                    }
                });
    }


    /**
     * 获取所有摄像头列表
     */
    private void getCameraList() {

        if (cameraMarkers.size() > 0) {
            for (Marker marker : cameraMarkers) {
                marker.setVisible(true);
            }

            if (cameraLatLngs.size() > 0) {
                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraLatLngs), 200));
            }
            return;
        }


        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("curPage", 1);
        hashMap.put("pageSize", 100);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(Constant.getInstance().POST_CAMERA_LIST)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            VideoListBean2 videoListBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                videoListBean = new VideoListBean2();
                                videoListBean = gson.fromJson(result, VideoListBean2.class);
                                List<VideoListBean2.PageBean.ListBean> dataBeans = videoListBean.getPage().getList();

                                if (dataBeans != null) {
                                    if (dataBeans.size() > 0) {
                                        cameraLatLngs = new ArrayList<>();
                                        //所有摄像头
                                        ArrayList<BitmapDescriptor> cameraBayonetGif = new ArrayList<BitmapDescriptor>();
                                        ApplicationInfo appInfo2 = context.getApplicationInfo();
                                        for (int i = 1; i <= 24; i++) {
                                            int resID = getResources().getIdentifier("icon_camera_bayonet" + i, "mipmap", appInfo2.packageName);
                                            cameraBayonetGif.add(BitmapDescriptorFactory.fromResource(resID));
                                        }


                                        for (int i = 0; i < dataBeans.size(); i++) {
                                            VideoListBean2.PageBean.ListBean listBean = dataBeans.get(i);
                                            String latitude = listBean.getLat();
                                            String longitude = listBean.getLng();
                                            String status = listBean.getStatus();
                                            String name = listBean.getName();

                                            if (StringUtils.isEmpty(status)) {
                                                status = "1"; //为空的话 暂时赋值为离线状态
                                            }

                                            //过滤掉经纬度为空的数据
                                            if (!StringUtils.isEmpty(latitude) && !StringUtils.isEmpty(longitude)) {
                                                double lat = Double.parseDouble(latitude);
                                                double lng = Double.parseDouble(longitude);
                                                LatLng location = new LatLng(lat, lng);

                                                //添加Marker
                                                if (status.equals("0")) {
                                                    Marker marker = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions2(location, name, INFO_WINDOW_OFF_SET_Y, cameraBayonetGif));
                                                    cameraMarkers.add(marker);
                                                    cameraLatLngs.add(location);
                                                } else if (status.equals("1")) {

                                                    Marker marker = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions1(location, name, INFO_WINDOW_OFF_SET_Y, R.mipmap.icon_camera_bayonet_offline));
                                                    cameraMarkers.add(marker);
                                                    cameraLatLngs.add(location);
                                                }
                                            }

                                            if (cameraLatLngs.size() > 0) {
                                                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(cameraLatLngs), MAP_FACTORY_PADDING));
                                            }

                                        }
                                    }
                                }

                            } catch (JsonSyntaxException e) {

                                e.printStackTrace();

                                ToastUtils.showLong(getResources().getString(R.string.text_json_error));

                            }


                        }
                    }
                });
    }

    /**
     * 红外设备
     */
    private void getInfraredDequiList() {
        if (infraredMarker.size() > 0) {

            for (Marker marker : infraredMarker) {
                marker.setVisible(true);
            }

            if (infraredLatLngs.size() > 0) {
                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(infraredLatLngs), 200));
            }
            return;
        }


        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("curPage", 1);
        hashMap.put("pageSize", 500);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_INF_DEQUI_LIST)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            InfraredDequiBean infraredDequiBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                infraredDequiBean = new InfraredDequiBean();
                                infraredDequiBean = gson.fromJson(result, InfraredDequiBean.class);
                                List<InfraredDequiBean.DataBean.ListBean> dataBeans = infraredDequiBean.getData().getList();
                                if (dataBeans != null) {
                                    int size = dataBeans.size();
                                    if (size > 0) {
                                        infraredLatLngs = new ArrayList<>();

                                        ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>();
                                        ApplicationInfo appInfo = context.getApplicationInfo();
                                        for (int i = 1; i <= 24; i++) {
                                            int resID = getResources().getIdentifier("icon_infrared" + i, "mipmap", appInfo.packageName);
                                            giflist.add(BitmapDescriptorFactory.fromResource(resID));
                                        }

                                        for (int i = 0; i < size; i++) {
                                            InfraredDequiBean.DataBean.ListBean listBean = dataBeans.get(i);
                                            String latitude = listBean.getLatitude();
                                            String longitude = listBean.getLongitude();
                                            String cameraName = listBean.getCameraName();
                                            int status = listBean.getStatus(); //状态 2：离线  1：在线
                                            if (!StringUtils.isEmpty(latitude) && !StringUtils.isEmpty(longitude)) {
                                                double lat = Double.parseDouble(latitude);
                                                double lng = Double.parseDouble(longitude);
                                                LatLng location = new LatLng(lat, lng);
                                                infraredLatLngs.add(location);
                                                //添加Marker
                                                if (status == 1) {
                                                    Marker marker = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions2(location, cameraName, INFO_WINDOW_OFF_SET_Y, giflist));
                                                    infraredMarker.add(marker);
                                                } else if (status == 2) {
                                                    ;
                                                    Marker marker = aMap.addMarker(MarkerOptionsUtils.getMarkerOptions1(location, cameraName, INFO_WINDOW_OFF_SET_Y, R.mipmap.icon_infrared_offline));
                                                    infraredMarker.add(marker);
                                                }
                                            }
                                        }
                                        if (infraredLatLngs.size() > 0) {
                                            aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(infraredLatLngs), MAP_FACTORY_PADDING));
                                        }
                                    }
                                }
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                                ToastUtils.showLong(getResources().getString(R.string.text_json_error));
                            }
                        }
                    }
                });
    }


    /**
     * 获得保护站数据
     */
    private void getDeptList() {

        if (projectDeptMarkers.size() > 0) {

            for (Marker marker : projectDeptMarkers) {
                marker.setVisible(true);
            }

            if (projectDeptLatLngs.size() > 0) {
                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(projectDeptLatLngs), 200));
            }
            return;
        }


        String JsonData = new GetJsonDataUtil().getJson(getActivity(), "projectDept.json");//获取assets目录下的json文件数据

        try {
            Gson gson = GsonFactory.getSingletonGson();
            ProjectDeptBean projectDeptBean = new ProjectDeptBean();
            projectDeptBean = gson.fromJson(JsonData, ProjectDeptBean.class);
            List<ProjectDeptBean.ListBean> listBeans = projectDeptBean.getList();

            projectDeptLatLngs = new ArrayList<>();

            for (int i = 0; i < listBeans.size(); i++) {
                ProjectDeptBean.ListBean listBean = listBeans.get(i);
                String deptName = listBean.getKey();
                List<Double> position = listBean.getPosition();
                double lng = position.get(0);
                double lat = position.get(1);
                LatLng location = new LatLng(lat, lng);
                projectDeptLatLngs.add(location);
                MarkerOptions markerOptions = new MarkerOptions()
                        .position(location)
                        .title(deptName)
                        .setInfoWindowOffset(0, INFO_WINDOW_OFF_SET_Y)
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_project_dept));
                Marker marker = aMap.addMarker(markerOptions);
                projectDeptMarkers.add(marker);

                if (projectDeptLatLngs.size() > 0) {
                    aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(projectDeptLatLngs), MAP_FACTORY_PADDING));
                }
            }

        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }


    }


    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    public void setOnStatusListener(OnStatusListener onStatusListener) {
        this.onStatusListener = onStatusListener;
    }


    public static interface OnCheckedChangeListener {

        void onCheckedChanged(RadioGroup group, int checkedId);
    }

    public static interface OnStatusListener {
        void onDismiss();

        void onShow();
    }

}
